package com.tommy.ddp.box
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.definition.AxisConfig;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.manager.SwapManager;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import starling.display.Button;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.textures.Texture;

	/**
	 * 对对碰的方块
	 * @author tommy
	 * 
	 */	
	public class DdpBox extends Sprite
	{
		private var _row:int;
		private var _column:int;
		private var _type:uint;
		
		private var btn:Button;
		
		private var isDown:Boolean
		
		private var orgPoint:Point;
		
		private var targetRow:int;
		private var targetY:int;
		
		public function DdpBox()
		{
			
		}
		
		
		public function set type(n:uint):void
		{
			_type = n;
			
			var color:uint;
			if(n == 0)
				color = 0xff00cc;
			else if(n == 1)
				color = 0xffcc00;
			else if(n == 2)
				color = 0x00ffcc;
			//
			var s:Shape = new Shape();
			s.graphics.beginFill(color);
			s.graphics.drawCircle(5,5,DDPConfig.GRID_H/2 -10/2);
			s.graphics.endFill();
			var rect:Rectangle = s.getBounds(s);
			var bmd:BitmapData = new BitmapData(s.width,s.height,true,0);
			var matrix:Matrix = new Matrix(1,0,0,1,-rect.x,-rect.y);
			bmd.draw(s,matrix);
			
			btn = new Button(Texture.fromBitmapData(bmd));
			btn.addEventListener(TouchEvent.TOUCH,onTouch);
			addChild(btn);
		}
		/**
		 * 移动到某个行（下落效果）
		 * @param i
		 * 
		 */		
		public function moveRow(i:int):void
		{
			if(i == this.row)
				return;
			//
			targetRow = i;
			targetY = targetRow*DDPConfig.GRID_H;
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void
		{
			this.y += DDPConfig.MOVE_ROW_SPEED;
			if(this.y >= targetY)
			{
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				row = targetRow;
				GridDataManager.regBox(column,row,this);
			}
		}
		
		private function onTouch(event:TouchEvent):void
		{
			var touch:Touch = event.getTouch(stage);
			if(!touch)
				return;
			//
			if(touch.phase == TouchPhase.BEGAN)
			{
				isDown = true;
				orgPoint = new Point(touch.globalX,touch.globalY);
			}
			else if(touch.phase == TouchPhase.ENDED)
			{
				isDown = false;
			}
			else if(touch.phase == TouchPhase.MOVED)
			{
				if(isDown)
				{
					checkMovePos(touch);
				}
			}
		}
		
		public function get type():uint
		{
			return _type;
		}
		
		/**
		 * 第几行/Y
		 * @param v
		 * 
		 */		
		public function set row(v:int):void
		{
			_row = v;
			this.y = _row*DDPConfig.GRID_H;
		}
		/**
		 * 第几列/X
		 * @param v
		 * 
		 */		
		public function set column(v:int):void
		{
			_column = v;
			this.x = _column*DDPConfig.GRID_W;
		}
		
		public function get row():int
		{
			return _row;
		}
		public function get column():int
		{
			return _column;
		}
		
		override public function dispose():void
		{
			/*this.scaleX = 1.5;
			return;*/
			super.dispose();
			
			GridDataManager.removeBox(this);
			
			this.removeFromParent();
			if(btn)
				btn.removeEventListener(TouchEvent.TOUCH,onTouch);
			btn = null;
			orgPoint = null;
		}
		//
		
		private function checkMovePos(touch:Touch):void
		{
			var axis:int;
			var dir:int;
			
			if(Math.abs(touch.globalX - orgPoint.x) > DDPConfig.GRID_W - 10)
			{
				axis = AxisConfig.X_AXIS;
				
				if(touch.globalX < orgPoint.x)
					dir = -1;
				else
					dir = 1;
				//
				isDown = false;
				SwapManager.swapBox(this,axis,dir);
			}
			else if(Math.abs(touch.globalY - orgPoint.y) > DDPConfig.GRID_H - 10)
			{
				axis = AxisConfig.Y_AXIS;
				
				if(touch.globalY < orgPoint.y)
					dir = -1;
				else
					dir = 1;
				//
				isDown = false;
				SwapManager.swapBox(this,axis,dir);
			}
		}
	}
}